import 'package:flutter/material.dart';
import '../homeView/search_top_view.dart'; // 搜索头部组件
import '../../services/home_info_manager.dart'; // 数据管理
import '../../manger/cached_image_widget.dart';
import 'package:ninetyfour/l10n/app_localizations.dart';
import '../homeController/search_detail_page.dart';
import '../../class/controller/product_detail.dart';
import '../../manger/user_manger.dart';
import '../../login/controller/login_view.dart';

class SearchPage extends StatelessWidget {
  const SearchPage({super.key});

  @override
  Widget build(BuildContext context) {
    final homeManager = HomeInfoManager();
    final hotInitInfo = homeManager.homeInitModel!;

    return Scaffold(
      backgroundColor: Colors.white,
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 搜索框
          SearchTopView(
            onSearch: (searchText) {
              if (!UserInfos.shared.token.isNotEmpty) {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => const LoginView()),
                );
              } else {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => SearchDetailPage(serachKey: searchText)),
                );
              } 
            },
            onBack: () {
              Navigator.pop(context);
            },
          ),

          const SizedBox(height: 15),
          // 热门搜索标题
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 20),
            child: Text(
              AppLocalizations.of(context)!.hotSearches,
              style: TextStyle(fontSize: 15, color: Colors.black87),
            ),
          ),

          const SizedBox(height: 10),
          // 热门搜索按钮列表
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 20),
            child: Wrap(
              spacing: 15,
              runSpacing: 10,
              children: hotInitInfo.hotSearchList.take(3).map((model) {
                return ElevatedButton(
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(builder: (context) => SearchDetailPage(serachKey: model.name)),
                    );
                  },
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Color(0xFFF8F6F7), // 按钮背景色
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(20),
                    ),
                    elevation: 0,
                    padding: const EdgeInsets.symmetric(
                      horizontal: 15, vertical: 10),
                  ),
                  child: Text(model.name, style: TextStyle(fontSize: 12, color: Colors.black87),),
                );
              }).toList(),
            ),
          ),
          const SizedBox(height: 20),
          // 热门商品标题
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 20),
            child: Text(
              AppLocalizations.of(context)!.hotProduct,
              style: TextStyle(fontSize: 15, color: Colors.black87),
            ),
          ),
          // 热门商品列表
          Expanded(
            child: Padding(
              padding: const EdgeInsets.only(left: 15, right: 15, top: 0),
              child: GridView.builder(
                padding: EdgeInsets.only(top: 15), // 移除默认的 padding
                itemCount: hotInitInfo.hotGoodsList.length,
                gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 4, // 每行 3 个
                  mainAxisSpacing: 10,
                  crossAxisSpacing: 0,
                  childAspectRatio: 0.9, // 调整宽高比减少间距
                ),
                itemBuilder: (context, index) {
                  final model = hotInitInfo.hotGoodsList[index];
                  return GestureDetector(
                    onTap: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => ProductDetailPage(
                            spuId: model.id,
                          ),
                        ),
                      );
                    },
                    child: Column(
                      children: [
                        // 商品图片
                        ClipRRect(
                          borderRadius: BorderRadius.circular(10),
                          child: CachedImage(
                            width: 60, 
                            height: 60,  
                            imageUrl: model.pic,
                            fit: BoxFit.cover,
                            placeholderPath: 'assets/images/placeholderImage.png',  // 占位符图片路径 
                          ),
                        ),
                        const SizedBox(height: 8),
                        // 商品名称
                        Text(
                          model.name,
                          maxLines: 2,
                          overflow: TextOverflow.ellipsis,
                          textAlign: TextAlign.center,
                          style: TextStyle(fontSize: 13, color: Colors.black87),
                        ),
                      ],
                    ),
                  );
                },
              ),
            ),
          ),
        ],
      ),
    );
  }
}
